package cn.codergege.demo.config;

import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.*;

public class Chapt11JpaHibernateWebAppInitializer
    extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] { RootConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] { WebConfig.class };
    }

    // 将 DispatcherServlet 映射到 "/"
    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

    // 配置 filters
    @Override
    protected Filter[] getServletFilters() {
        return new Filter[] { new HiddenHttpMethodFilter() };
    }

    @Override
    protected void customizeRegistration(ServletRegistration.Dynamic registration) {
        // 路径: 传入 null 会使用　"" 路径
        // 限制文件大小不超过 2 Mb, 整个请求不超过 4 Mb
        // 在上传过程中, 文件大小达到一个指定的最大容量(以字节为单位), 将写到临时文件路径. 0 表示所有上传文件都会写入磁盘.
        registration.setMultipartConfig(
                new MultipartConfigElement(null,
                        2 * 1024 * 1024,
                        4 * 1024 * 1024,
                        0));
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        servletContext.setInitParameter("spring.profiles.default", "development");
//        servletContext.setInitParameter("spring.profiles.active", "production");
//        servletContext.setInitParameter("targetFilterLifecycle", "false");
        super.onStartup(servletContext);
    }
}
			